/**
 * 定义ui公共模块
 */
(function(window, angular, undefined){

	var component = angular.module('water.common.component.CustomerDomainSingleCheck',['water.entity.domain','water.common.formCheck']);
	/**
	 * 自定义字典check组件
	 */
	component.directive("comCustomerDomainSingleCheck", function($domainService,$q) {
		return { 
			transclude: false,    // 不复制原始HTML内容 
		    restrict: "A",        // 指令是一个元素 (并非属性) 
		    require : "ngModel",
		    replace: true,        // 使用模板替换原始标记 
		    scope: {              // 设置指令对于的scope 
		      model:"=ngModel",
		      uiType:"=uiType",
		      tableName:"@tableName",
		      keyField:"@keyField",
		      valueField:"@valueField",
		      parentField:"@parentField",
		      componentObject:"=componentObject",
		      childComponentObject:"=childComponentObject",
		      parentComponentObject:"=parentComponentObject",
		      getData:'&getData',
		      change:"&ngChange"
		    }, 
		    template: function( element, attrs){
		    	return '<div><com-single-check ng-change="change()" ng-model="model" data="data" component-object="comSelectComponent"></com-mulit-check></div>';
		    },
		    link: function ($scope, element, attrs, controller) {
		    	$scope.comSelectComponent;
		    	try {
		    		$scope.componentObject = {
			    			name:attrs.ngModel,
			    			setData : function(data){
			    				$scope.comSelectComponent.setData(data);
			    			},
			    			updateByParentId : function(pid){
			    				$scope.getDomainData(pid).then(function(data){
			    					$scope.comSelectComponent.setData(data);
			    				});
			    			}
			    	};
				} catch (e) {
					console.log(e)
				}
		    		
		    	
		    	/**
		    	 * 获取字典数据，并转化成组件支持的数据格式
		    	 */
		    	$scope.getDomainData = function(id){
		    		var deferred = $q.defer();
					var promise = deferred.promise;
					
					if($scope.getData && $scope.getData()){
		    			$scope.getData()(id).then(function(d){
		    				if(!d){
		    					deferred.resolve([]);
		    				}
		    				else{
		    					var data = [];
					    		for(var i=0;i<d.length;i++){
					    			data.push({
					    				v:d[i][$scope.valueField],
					    				k:d[i][$scope.keyField]
					    			});
					    		}
					    		deferred.resolve(data);
		    				}
		    			});
		    		}
					else{
						$domainService.getCustomersList($scope.tableName,$scope.keyField,$scope.valueField,$scope.parentField,id).then(function(d){
				    		var data = [];
				    		for(var i=0;i<d.length;i++){
				    			data.push({
				    				v:d[i].V,
				    				k:d[i].K
				    			});
				    		}
				    		deferred.resolve(data);
				    	});
					}
						
		    		return promise;
		    	}
		    	
		    	/**
		    	 * 初始化组件数据
		    	 */
		    	function initData(){
	    			$scope.getDomainData($scope.parentId).then(function(data){
		    			$scope.data = data;
		    		});
		    	}
		    	
		    	/**
		    	 * 初始化组件数据
		    	 */
		    	function initData(){
	    			$scope.getDomainData($scope.parentId).then(function(data){
		    			$scope.data = data;
		    		});
	    			if(attrs.childComponentObject){
	    				var dereg = $scope.$watch('childComponentObject', function(){
	    					if($scope.childComponentObject){
	    						updateChildComponentData();
	    						dereg();
	    					}
	    				});
	    			}
		    	}
		    	
		    	/**
		    	 * 更新子控件的数据
		    	 */
		    	function updateChildComponentData(){
		    		if($scope.model && $scope.childComponentObject){
		    			$scope.childComponentObject.updateByParentId($scope.model);
		    		}
		    	}
		    	
		    	controller.$render = function(){
		    		$scope.model = controller.$viewValue;
		    		updateChildComponentData();
		    	}
		    	
		    	initData();
		    }
		  } 
	});
}(window, angular));